#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<arpa/inet.h>

using namespace std;



int main(int argc, char **argv)
{
	int sockfd;
	char buf[32] = {0};
	socklen_t len = sizeof(struct sockaddr_in);

	struct sockaddr_in serveraddr;
	memset(&serveraddr, 0, len);

	//fill the info of the server
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_port = htons(atoi(argv[2]));
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);

	//note: use SOCK_DGRAM, because of the using of udp.
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if(sockfd < 0)
	{
		perror("fail to socket");
		return -1;	
	}

	strcpy(buf, "I am client");
	sendto(sockfd, buf, 32, 0, (struct sockaddr*)&serveraddr, len);
	recvfrom(sockfd, buf, 32, 0, NULL, NULL);
	printf("%s\n", buf);

	close(sockfd);

	return 0;
}
